package com.niuwa.p2p.service.impl.productTemplate;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.DictConstant;
import com.niuwa.p2p.core.utils.DictConstant.ProductIdType;
import com.niuwa.p2p.dao.business.LoanClaimDao;
import com.niuwa.p2p.dao.business.ProdAuditDao;
import com.niuwa.p2p.dao.productTemplate.ProductDao;
import com.niuwa.p2p.dao.productTemplate.TemplateAuditDao;
import com.niuwa.p2p.dao.user.UserDao;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.entity.business.DirectProduct;
import com.niuwa.p2p.entity.business.LoanClaim;
import com.niuwa.p2p.entity.business.ProdAudit;
import com.niuwa.p2p.entity.claimtransfer.ClaimProductVo;
import com.niuwa.p2p.entity.productTemplate.Product;
import com.niuwa.p2p.entity.productTemplate.TemplateAudit;
import com.niuwa.p2p.entity.user.User;
import com.niuwa.p2p.service.business.IDirectProductService;
import com.niuwa.p2p.service.productTemplate.ProductService;
import com.niuwa.p2p.service.sequence.ISequenceService;
import com.niuwa.p2p.vo.business.ProductVo;

@Service
public class ProductServiceImpl implements ProductService {
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private ProdAuditDao prodAuditDao;
	
	@Autowired
	private IDirectProductService directProductService;

	@Autowired
	private ISequenceService sequenceService;
	

	@Autowired
	private TemplateAuditDao templateAuditDao;
	

	@Autowired
	private LoanClaimDao loanClaimDao;
	
	@Override
	public List<ProductVo> selectProductByTempId(Long tempId) {
		return productDao.getProductByTempId(tempId);
	}

	
	@Override
	public PageBean<ProductVo> search(Map<String, Object> paramMap) throws Exception {
		
		int totalCount = productDao.count(paramMap);

		PageBean<ProductVo> page = new PageBean<ProductVo>(totalCount);
		// 设置当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());

		List<ProductVo> dataList  = productDao.search(paramMap);
		
		User operator;
		User auditor;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (ProductVo productVo : dataList) {
			if(productVo.getOperator() != null){
				operator = userDao.selectByUserId(productVo.getOperator());
				if(operator != null){
					productVo.setOperatorName(operator.getUserName());
				}
			}
			if(productVo.getAuditor() != null){
				auditor = userDao.selectByUserId(productVo.getAuditor());
				if(auditor != null){
					productVo.setAuditorName(auditor.getUserName());
				}
			}
			//如果项目发布状态为未通过，需要查询原因
			if(DictConstant.ReleaseStatus.NO_PASS.getCode().equals(productVo.getReleaseStatus())){
				ProdAudit prodAudit = prodAuditDao.getLatestProdAudit(productVo.getProductCode());
				
				if(prodAudit != null){
					productVo.setUnpassOperator(userDao.selectByUserId(prodAudit.getOperator()).getUsername());
					productVo.setUnpassOperateTime(sdf.format(prodAudit.getCreateTime()));
					productVo.setUnpassRemark(prodAudit.getRemark());
				}
				
			}
		}
		
		page.setRows(dataList);

		return page;
	}
	
	@Override
	public boolean addDirectProduct(String projectNo) throws Exception {
		int dpCount=directProductService.getCountByProjectNo(projectNo);
		if(dpCount==0){//新流程
			String productCode=getNewProductId(ProductIdType.CPZT);//直投类
			Product product=new Product(productCode);
			int count1 = directProductService.addDirectProduct(new DirectProduct(product,projectNo));//通过DirectProduct的唯一索引保证并发。
			int count2=0;
			if(count1>0){
				//start 	直投发布产品名称默认为项目名称 add by money
				LoanClaim loanClaim = loanClaimDao.selectByPrimaryKey(projectNo);
				if(loanClaim != null){
					product.setProductName(loanClaim.getProjectName());
				}
				//end
				count2=productDao.addProduct(product);
			}
			return (count1>0 && count2>0);
		}
		return true;//已有直投产品复用
	}


	@Override
	public int updateProduct(Product product) throws Exception {
		return productDao.updateProduct(product);
	}



	
	@Override
	public String getNewProductId(ProductIdType type) throws Exception {
		String code=type.getCode();
		long seq=sequenceService.getNextSequenceBySeqKey(code);
		return code+ String.format("%020d", seq);    //产品类型+20位固定长度的序列   
	}
	
	


	/**
	 * 获取最新产品模块审核记录
	 */

	@Override
	public TemplateAudit getNewstAudit(Integer tempId) {
		
		return templateAuditDao.getNewestAudit(tempId);
	}


	@Override
	public ClaimProductVo getTransferClaimProduct(String creditNo) {
		// TODO Auto-generated method stub
		return productDao.getTransferClaimProduct(creditNo);
	}


}
